package com.wnxy.sddfp.bank.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.sddfp.bank.entity.TradeLog;
import com.wnxy.sddfp.bank.service.ITradeLogService;
import com.wnxy.sddfp.common.constant.MQConstant;
import com.wnxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.wnxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDto;
import com.wnxy.sddfp.common.model.vo.trade.BorrowSyncRecordVo;
import com.wnxy.sddfp.common.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2024-01-17
 */
@RestController
public class TradeLogController {

    private final ITradeLogService tradeLogService;
    private final RabbitTemplate rabbitTemplate;
    public TradeLogController(ITradeLogService tradeLogService, RabbitTemplate rabbitTemplate) {
        this.tradeLogService = tradeLogService;
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 绑卡
     */
    @PostMapping("/trade-log/bank/access/card")
    public SignatureDto proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        SignatureDto signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }

    /**
     * 银行托管系统回调接口
     */
    @PostMapping("/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto) {
        // 根据流水号，修改交易记录的同步状态:synchronize_status
        tradeLogService.update(Wrappers.lambdaUpdate(TradeLog.class)
                .set(TradeLog::getSynchronizeStatus,1)
                .eq(TradeLog::getReqCode,bankCardRequestDto.getReqCode())
        );
        // 发送MQ消息，通知系统管理服务绑卡结果
        rabbitTemplate.convertAndSend(
                MQConstant.SDDFP_EXCHANGE_BIND,MQConstant.BIND_CARD_MESSAGE,bankCardRequestDto);
        return "ok";
    }

    /**
     * 借款审核,同步借款数据
     */
    @ApiOperation("借款审核,同步借款数据")
    @PostMapping("/bank/access/sync/borrow")
    public Result sysncBorrowRecord(@RequestBody BorrowSyncRecordVo borrowSyncRecordVo) {
        tradeLogService.sysncBorrowRecord(borrowSyncRecordVo);
        return Result.ok();
    }
}
